package com.test15.fastjson;

import java.util.ArrayList;
import java.util.HashMap;

import org.junit.Before;
import org.junit.Test;

import com.alibaba.fastjson.JSON;
import com.test09.gson01.CourseBean;
import com.test09.gson01.StudentBean;

/*
 * 不想被转json的使用transient来修饰字段就行
 */
public class JsonDemo {
	private ArrayList<StudentBean> studentBeans;
	private String json;
	/**
	 * 把对象转成json，后面的true，只是为了格式化这个json
	 */
	@Test
	public void demo01(){
		this.json = JSON.toJSONString(this.studentBeans,true);
		System.out.println(this.json);
	}
	
	
	/**
	 * 把json转对象
	 */
	@Test
	public void demo02(){
		demo01();
		
		ArrayList<StudentBean> lists = JSON.parseObject(this.json, this.studentBeans.getClass());
		System.out.println(lists);
	}
	
	
	
	@Before
	public void init(){
//		创建对象studentBean1，并填充属性
		StudentBean studentBean1 = new StudentBean();
		
		studentBean1.setName("wang1");
		studentBean1.setAge(10);
		
		ArrayList<CourseBean> arrayList = new ArrayList<CourseBean>();
		arrayList.add(new CourseBean("eng"));
		arrayList.add(new CourseBean("math"));
		studentBean1.setCourseBeans(arrayList);
		
		HashMap<String, CourseBean> map = new HashMap<>();
		map.put("k1", new CourseBean("eng"));
		map.put("k2", new CourseBean("math"));
		studentBean1.setMap(map);
		
		
//		创建对象studentBean2，并填充属性
		StudentBean studentBean2 = new StudentBean();
		
		studentBean2.setName("wang2");
		studentBean2.setAge(10);
		
		ArrayList<CourseBean> arrayList2 = new ArrayList<CourseBean>();
		arrayList2.add(new CourseBean("eng"));
		arrayList2.add(new CourseBean("math"));
		studentBean2.setCourseBeans(arrayList2);
		
		HashMap<String, CourseBean> map2 = new HashMap<>();
		map2.put("k1", new CourseBean("eng"));
		map2.put("k2", new CourseBean("math"));
		studentBean2.setMap(map2);
		
//		加入到list中
		ArrayList<StudentBean> arrayList3 = new ArrayList<>();
		arrayList3.add(studentBean1);
		arrayList3.add(studentBean2);
		
		this.studentBeans = arrayList3;
	}
}
